import {
  observable, autorun, makeObservable, computed, action, flow,
  makeAutoObservable, reaction, when, runInAction
} from 'mobx';

class Doubler {
  value
  age = 100
  constructor(value) {
    makeAutoObservable(this, { PI: false }, { autoBind: true });
    this.value = value;
  }
  get double() {
    return this.value * 2;
  }
  //action有很多好处 1.action的内部会使用事务的机制 2.只在action修改状态，好排查错误
  add() {
    this.value++;
    this.value++;
  }
  *fetch() {//redux-saga
    let amount = yield new Promise((resolve) => setTimeout(() => resolve(5), 1000));
    this.value += amount;
  }
}
let doubler = new Doubler(1);
//不会立刻执行，只会在值发生改变后执行
/* reaction(
  () => doubler.value,//data函数，它会返回一个值
  (value, previousValue) => console.log(value, previousValue)//effect函数
);
doubler.value = 2; */

//let disposer = when(() => doubler.value > 3, () => console.log(doubler.value));
autorun(() => console.log(doubler.value));
runInAction(() => {
  doubler.value++;//2
  doubler.value++;//3
  doubler.value++;//4
  doubler.value++;//5
})

